package com.zbkc.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;

public class StarCloudUtil {

	public static String MD5(String string) {
		byte[] hash;
		try {
			hash = MessageDigest.getInstance("MD5").digest(
					string.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("Huh, MD5 should be supported?", e);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("Huh, UTF-8 should be supported?", e);
		}

		StringBuilder hex = new StringBuilder(hash.length * 2);
		for (byte b : hash) {
			if ((b & 0xFF) < 0x10)
				hex.append("0");
			hex.append(Integer.toHexString(b & 0xFF));
		}
		return hex.toString();
	}

	public static boolean existSDCard() {
		return Environment.getExternalStorageState().equals("mounted");
	}

	public static Intent getAllIntent(String paramString) {
		Intent localIntent = new Intent();
		localIntent.addFlags(268435456);
		localIntent.setAction("android.intent.action.VIEW");
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)), "*/*");
		return localIntent;
	}

	public static Intent getApkFileIntent(String paramString) {
		Intent localIntent = new Intent();
		localIntent.addFlags(268435456);
		localIntent.setAction("android.intent.action.VIEW");
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"application/vnd.android.package-archive");
		return localIntent;
	}

	public static Intent getAudioFileIntent(String paramString) {
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.addFlags(67108864);
		localIntent.putExtra("oneshot", 0);
		localIntent.putExtra("configchange", 0);
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"audio/*");
		return localIntent;
	}

	public static Intent getChmFileIntent(String paramString) {
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.addCategory("android.intent.category.DEFAULT");
		localIntent.addFlags(268435456);
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"application/x-chm");
		return localIntent;
	}

	public static String getClientDeviceName() {
		return Build.MODEL;
	}

	public static String getClientVersion() {
		return Build.VERSION.RELEASE;
	}

	public static Intent getExcelFileIntent(String paramString) {
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.addCategory("android.intent.category.DEFAULT");
		localIntent.addFlags(268435456);
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"application/vnd.ms-excel");
		return localIntent;
	}

	// public static String getHostIp() {
	// try {
	// InetAddress localInetAddress;
	// do {
	// Enumeration localEnumeration1 = NetworkInterface
	// .getNetworkInterfaces();
	// while (!localEnumeration1.hasMoreElements()) {
	// boolean bool = localEnumeration1.hasMoreElements();
	// if (!bool)
	// return "ip";
	// localEnumeration1 = ((NetworkInterface) localEnumeration1
	// .nextElement()).getInetAddresses();
	// }
	// localInetAddress = (InetAddress) localEnumeration1
	// .nextElement();
	// } while (localInetAddress.isLoopbackAddress());
	// String str = localInetAddress.getHostAddress();
	// return str;
	// } catch (Exception localException) {
	// localException.printStackTrace();
	// }
	// }

	public static Intent getHtmlFileIntent(String paramString) {
		Uri localUri = Uri.parse(paramString).buildUpon()
				.encodedAuthority("com.android.htmlfileprovider")
				.scheme("content").encodedPath(paramString).build();
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.setDataAndType(localUri, "text/html");
		return localIntent;
	}

	public static Intent getImageFileIntent(String paramString) {
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.addCategory("android.intent.category.DEFAULT");
		localIntent.addFlags(268435456);
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"image/*");
		return localIntent;
	}

	public static Intent getPdfFileIntent(String paramString) {
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.addCategory("android.intent.category.DEFAULT");
		localIntent.addFlags(268435456);
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"application/pdf");
		return localIntent;
	}

	public static Intent getPptFileIntent(String paramString) {
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.addCategory("android.intent.category.DEFAULT");
		localIntent.addFlags(268435456);
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"application/vnd.ms-powerpoint");
		return localIntent;
	}

	public static Intent getTextFileIntent(String paramString,
			boolean paramBoolean) {
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.addCategory("android.intent.category.DEFAULT");
		localIntent.addFlags(268435456);
		if (paramBoolean) {
			localIntent.setDataAndType(Uri.parse(paramString), "text/plain");
			return localIntent;
		}
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"text/plain");
		return localIntent;
	}

	public static Intent getVideoFileIntent(String paramString) {
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.addFlags(67108864);
		localIntent.putExtra("oneshot", 0);
		localIntent.putExtra("configchange", 0);
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"video/*");
		return localIntent;
	}

	public static Intent getWordFileIntent(String paramString) {
		Intent localIntent = new Intent("android.intent.action.VIEW");
		localIntent.addCategory("android.intent.category.DEFAULT");
		localIntent.addFlags(268435456);
		localIntent.setDataAndType(Uri.fromFile(new File(paramString)),
				"application/msword");
		return localIntent;
	}

	public static Intent openFile(String paramString) {
		File localFile = new File(paramString);
		String str = localFile
				.getName()
				.substring(1 + localFile.getName().lastIndexOf("."),
						localFile.getName().length()).toLowerCase();
		LogUtil.e("扩展名:" + str);
		if ((str.equals("m4a")) || (str.equals("mp3")) || (str.equals("mid"))
				|| (str.equals("xmf")) || (str.equals("ogg"))
				|| (str.equals("wav")))
			return getAudioFileIntent(paramString);
		if ((str.equals("3gp")) || (str.equals("mp4")))
			return getAudioFileIntent(paramString);
		if ((str.equals("jpg")) || (str.equals("gif")) || (str.equals("png"))
				|| (str.equals("jpeg")) || (str.equals("bmp")))
			return getImageFileIntent(paramString);
		if (str.equals("apk"))
			return getApkFileIntent(paramString);
		if (str.equals("ppt"))
			return getPptFileIntent(paramString);
		if (str.equals("xls"))
			return getExcelFileIntent(paramString);
		if (str.equals("doc"))
			return getWordFileIntent(paramString);
		if (str.equals("pdf"))
			return getPdfFileIntent(paramString);
		if (str.equals("chm"))
			return getChmFileIntent(paramString);
		if (str.equals("txt"))
			return getTextFileIntent(paramString, false);
		return getAllIntent(paramString);
	}

}
